﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Wallpaper.Data.Migrations
{
    /// <inheritdoc />
    public partial class InitialCreate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "admins",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "管理员主键ID"),
                    Account = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "管理员账号，用于登录"),
                    PasswordHash = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false, comment: "密码哈希值"),
                    AvatarUrl = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true, comment: "管理员头像URL"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP", comment: "创建时间"),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "更新时间"),
                    Status = table.Column<bool>(type: "bit", nullable: false, defaultValue: true, comment: "账号状态：true-启用,false-禁用"),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false, comment: "软删除标志：true-已删除,false-正常")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_admins", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "categories",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "分类主键ID"),
                    Name = table.Column<string>(type: "nvarchar(30)", maxLength: 30, nullable: false, comment: "分类名称"),
                    Sort = table.Column<int>(type: "int", nullable: false, defaultValue: 0, comment: "排序序号（数字越小越靠前）"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP", comment: "创建时间"),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_categories", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "download_records",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "下载记录主键ID"),
                    UserId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "下载用户ID"),
                    WallpaperId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "被下载的壁纸ID"),
                    PointsCost = table.Column<int>(type: "int", nullable: false, defaultValue: 1, comment: "本次下载消耗的积分"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP", comment: "下载时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_download_records", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "tags",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "标签主键ID"),
                    Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "标签名称"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP", comment: "创建时间"),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "最后更新时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_tags", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "user_favorites",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "收藏记录主键ID"),
                    UserId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "用户ID"),
                    WallpaperId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "被收藏的壁纸ID"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP", comment: "收藏时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_user_favorites", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "users",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "用户主键ID"),
                    OpenId = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: true, comment: "微信OpenId，用户唯一标识"),
                    Nickname = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: true, comment: "用户昵称"),
                    Signature = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: true, comment: "个性签名"),
                    PasswordHash = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Gender = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: true, comment: "性别：male-男,female-女,unknown-未知"),
                    AvatarUrl = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "头像图片URL地址"),
                    Points = table.Column<int>(type: "int", nullable: false, defaultValue: 0, comment: "用户积分"),
                    TodayFreeDownloadCount = table.Column<int>(type: "int", nullable: false, defaultValue: 3, comment: "今日剩余免费下载次数（每天重置）"),
                    LastDownloadDate = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP", comment: "最后下载日期（用于判断是否跨天重置免费次数）"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP", comment: "账号创建时间"),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "最后更新时间，自动维护"),
                    Status = table.Column<bool>(type: "bit", nullable: false, defaultValue: true, comment: "账号状态：true-启用,false-禁用"),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false, comment: "软删除标志：true-已删除,false-正常")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_users", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "wallpaper_and_tags",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "关联表主键ID"),
                    WallpaperId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "壁纸ID"),
                    TagId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "标签ID")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_wallpaper_and_tags", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "wallpapers",
                columns: table => new
                {
                    Id = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "壁纸主键ID"),
                    Title = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false, comment: "壁纸标题"),
                    FileUrl = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false, comment: "壁纸文件URL（原图地址）"),
                    ThumbnailUrl = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false, comment: "缩略图URL（用于列表展示）"),
                    UploaderId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "上传人ID（关联用户或管理员）"),
                    CategoryId = table.Column<string>(type: "nvarchar(36)", maxLength: 36, nullable: false, comment: "分类ID（关联分类表）"),
                    UploaderType = table.Column<int>(type: "tinyint", nullable: false, defaultValue: 0, comment: "上传人类型：0-用户，1-管理员"),
                    Width = table.Column<int>(type: "int", nullable: false, comment: "壁纸宽度（像素）"),
                    Height = table.Column<int>(type: "int", nullable: false, comment: "壁纸高度（像素）"),
                    ReviewStatus = table.Column<bool>(type: "tinyint", nullable: false, defaultValue: false, comment: "审核状态：true-审核通过，false-待审核/未通过"),
                    ReviewerId = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true, comment: "审核人ID"),
                    ReviewComment = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true, comment: "审核意见/理由（审核不通过时显示给用户）"),
                    ReviewedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "审核时间"),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP", comment: "创建时间（上传时间）"),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true, comment: "最后更新时间"),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false, defaultValue: false, comment: "软删除标志：true-已删除,false-正常")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_wallpapers", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "idx_categories_createdat",
                table: "categories",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "idx_categories_name",
                table: "categories",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "idx_categories_sort",
                table: "categories",
                column: "Sort");

            migrationBuilder.CreateIndex(
                name: "idx_downloadrecords_createdat",
                table: "download_records",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "idx_downloadrecords_userid",
                table: "download_records",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "idx_downloadrecords_wallpaperid",
                table: "download_records",
                column: "WallpaperId");

            migrationBuilder.CreateIndex(
                name: "idx_tags_name",
                table: "tags",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "idx_userfavorites_createdat",
                table: "user_favorites",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "idx_userfavorites_user_wallpaper_unique",
                table: "user_favorites",
                columns: new[] { "UserId", "WallpaperId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "idx_userfavorites_userid",
                table: "user_favorites",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "idx_userfavorites_wallpaperid",
                table: "user_favorites",
                column: "WallpaperId");

            migrationBuilder.CreateIndex(
                name: "idx_users_createdat",
                table: "users",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "idx_users_openid",
                table: "users",
                column: "OpenId",
                unique: true,
                filter: "[OpenId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "idx_users_status_deleted",
                table: "users",
                columns: new[] { "Status", "IsDeleted" });

            migrationBuilder.CreateIndex(
                name: "idx_wallpaper_tag_tag",
                table: "wallpaper_and_tags",
                column: "TagId");

            migrationBuilder.CreateIndex(
                name: "idx_wallpaper_tag_unique",
                table: "wallpaper_and_tags",
                columns: new[] { "WallpaperId", "TagId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "idx_wallpaper_tag_wallpaper",
                table: "wallpaper_and_tags",
                column: "WallpaperId");

            migrationBuilder.CreateIndex(
                name: "idx_wallpapers_categoryid",
                table: "wallpapers",
                column: "CategoryId");

            migrationBuilder.CreateIndex(
                name: "idx_wallpapers_createdat",
                table: "wallpapers",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "idx_wallpapers_reviewstatus",
                table: "wallpapers",
                column: "ReviewStatus");

            migrationBuilder.CreateIndex(
                name: "idx_wallpapers_status_created",
                table: "wallpapers",
                columns: new[] { "ReviewStatus", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "idx_wallpapers_uploader_created",
                table: "wallpapers",
                columns: new[] { "UploaderId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "idx_wallpapers_uploaderid",
                table: "wallpapers",
                column: "UploaderId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "admins");

            migrationBuilder.DropTable(
                name: "categories");

            migrationBuilder.DropTable(
                name: "download_records");

            migrationBuilder.DropTable(
                name: "tags");

            migrationBuilder.DropTable(
                name: "user_favorites");

            migrationBuilder.DropTable(
                name: "users");

            migrationBuilder.DropTable(
                name: "wallpaper_and_tags");

            migrationBuilder.DropTable(
                name: "wallpapers");
        }
    }
}
